package com.tannm.doan.taskmanager.reminder;

import com.tannm.doan.taskmanager.R;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import java.util.*;

import com.tannm.doan.taskmanager.*;

import android.content.ComponentName;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.*;
import android.widget.*;

public class AlarmDialog extends CommonActivity {
	// list adapter for alarm items
	// ---------------------------------------------------------
	public class AlarmsAdapter implements ListAdapter {
		// fields
		private ArrayList<AlarmDialogDataItem> vecAlarmItems = null;

		// methods
		AlarmsAdapter(ArrayList<AlarmDialogDataItem> vecAlarmItems) {
			this.vecAlarmItems = vecAlarmItems;
		}

		public int getCount() {
			return vecAlarmItems.size();
		}

		public Object getItem(int position) {
			return vecAlarmItems.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			return vecAlarmItems.get(position).getView();
		}

		public void registerDataSetObserver(DataSetObserver observer) {
		}

		public void unregisterDataSetObserver(DataSetObserver arg0) {
		}

		public boolean areAllItemsEnabled() {
			return true;
		}

		public boolean isEnabled(int position) {
			return true;
		}

		public int getItemViewType(int position) {
			return 0;
		}

		public int getViewTypeCount() {
			return 1;
		}

		public boolean hasStableIds() {
			return true;
		}

		public boolean isEmpty() {
			return (vecAlarmItems.size() == 0);
		}
	}

	// fields
	ArrayList<AlarmDialogDataItem> vecAlarmItems = new ArrayList<AlarmDialogDataItem>();

	// views
	private TextView labAlarmReminderHeader = null;
	private ListView listAlarmReminderContent = null;
	private ImageView imgAlarmReminderHeader = null;

	// views
	private Button btnAlarmReminderClear = null;
	private Button btnAlarmReminderSnooze = null;
	private Button btnAlarmReminderOpen = null;

	// fields
	private Bundle data = null;
	private Handler handlerCloseDialog = new Handler();
	private AlarmsManager alarmsManager = null;

	// fields
	private String msgYouHaveASTS = "";
	private String msgYouHaveTS = "";
	private String msgYouHaveAS = "";
	private String msgYouHaveNoASTS = "";

	// methods
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.alarmdialog);

		// create alarsm manager instance
		alarmsManager = new AlarmsManager(this.userdb, this.prefs);

		// localize strings
		msgYouHaveASTS = utils.getResStr(R.string.msgYouHaveASTS);
		msgYouHaveTS = utils.getResStr(R.string.msgYouHaveTS);
		msgYouHaveAS = utils.getResStr(R.string.msgYouHaveAS);
		msgYouHaveNoASTS = utils.getResStr(R.string.msgYouHaveNoASTS);
	}

	@Override
	public void onStart() {
		super.onStart();

		// get data from intent bundle
		data = this.getIntent().getExtras();
		getIntentDataFromBundle();

		// initialize views
		initViews();
		initState();
	}

	@Override
	public void onResume() {
		super.onResume();

	}

	@Override
	public void onPause() {
		super.onPause();

	}

	@Override
	public void onStop() {
		super.onStop();

		// process alarms
		alarmsManager.processAll();

		// send update service for refresh data
		TaskManager.updateReminderService(this, this.prefs,
				CommonActivity.bundleTableUpdated);
	}

	public String getPopupMsg(int iCountAppts, int iCountTasks) {
		String msg = String.format(msgYouHaveASTS, iCountAppts, iCountTasks);
		if ((iCountAppts == 0) && (iCountTasks > 0))
			msg = String.format(msgYouHaveTS, iCountTasks);
		if ((iCountTasks == 0) && (iCountAppts > 0))
			msg = String.format(msgYouHaveAS, iCountAppts);
		if ((iCountAppts == 0) && (iCountTasks == 0))
			msg = msgYouHaveNoASTS;
		return msg;
	}

	private void updateHeaderInfo() {
		int iCountAppts = 0;
		int iCountTasks = 0;

		for (int i = 0; i < vecAlarmItems.size(); i++) {
			AlarmDialogDataItem item = vecAlarmItems.get(i);
			if (item != null) {
				if (item.getOrderFilter() == AlarmDataViewItem.iOrderAppts)
					iCountAppts++;
				if (item.getOrderFilter() == AlarmDataViewItem.iOrderTasks)
					iCountTasks++;
			}
		}

		String sTitle = getPopupMsg(iCountAppts, iCountTasks);
		labAlarmReminderHeader.setText(sTitle);

		// update icon
		imgAlarmReminderHeader
				.setBackgroundResource(R.drawable.iconnotifyalarm);
	}

	private void getIntentDataFromBundle() {
		if (data != null) {
			vecAlarmItems.clear();
			decodeBundleData(vecAlarmItems, AlarmDataViewItem.iOrderAppts);
			decodeBundleData(vecAlarmItems, AlarmDataViewItem.iOrderTasks);
		}
	}

	private int decodeBundleData(ArrayList<AlarmDialogDataItem> items,
			int iOrderFilter) {
		int iTotalCount = 0;
		String sTotalKey = "typetotal_" + Integer.toString(iOrderFilter);
		if (data.containsKey(sTotalKey)) {
			iTotalCount = data.getInt(sTotalKey);
			for (int i = 0; i < iTotalCount; i++) {
				String sKey = "_" + Integer.toString(iOrderFilter) + "_"
						+ Integer.toString(i);

				long lID = data.getLong("rowid" + sKey);
				String sText = data.getString("text" + sKey);

				AlarmDialogDataItem item = new AlarmDialogDataItem(this,
						iOrderFilter, lID, sText);
				items.add(item);
			}
		}
		return iTotalCount;
	}

	private void initViews() {
		// get views
		labAlarmReminderHeader = (TextView) findViewById(R.id.labAlarmReminderHeader);
		listAlarmReminderContent = (ListView) findViewById(R.id.listAlarmReminderContent);
		imgAlarmReminderHeader = (ImageView) findViewById(R.id.imgAlarmReminderHeader);

		// buttons
		btnAlarmReminderClear = (Button) findViewById(R.id.btnAlarmReminderClear);
		btnAlarmReminderClear.setOnClickListener(mBtnOnClick_CLEAR);

		btnAlarmReminderSnooze = (Button) findViewById(R.id.btnAlarmReminderSnooze);
		btnAlarmReminderSnooze.setOnClickListener(mBtnOnClick_SNOOZE);

		btnAlarmReminderOpen = (Button) findViewById(R.id.btnAlarmReminderOpen);
		btnAlarmReminderOpen.setOnClickListener(mBtnOnClick_OPEN);
	}

	private void initState() {
		// title
		String sSubTitle = utils.getResStr(R.string.titleDefaultAlarmDialog);
		setActivityTitle(sSubTitle);

		updateHeaderInfo();

		// fill list
		setListWithDataAdapter();

		alarmsManager.clear();

		btnAlarmReminderClear.setFocusable(true);
		btnAlarmReminderClear.setFocusableInTouchMode(true);

		listAlarmReminderContent.setOnItemClickListener(mListViewOnItemClick);
		listAlarmReminderContent
				.setOnItemSelectedListener(mListViewOnItemSelected);

		selectFirstListItem();

		updateBottomButtonsState();

		btnAlarmReminderClear.requestFocus();
	}

	private void setListWithDataAdapter() {
		listAlarmReminderContent.setAdapter(new AlarmsAdapter(vecAlarmItems));
	}

	public void selectFirstListItem() {
		clearAllSelection();
		if (vecAlarmItems.size() > 0)
			vecAlarmItems.get(0).setSelected(true);
	}

	// android.widget.AdapterView.OnItemClickListener
	@SuppressWarnings("all")
	private AdapterView.OnItemClickListener mListViewOnItemClick = new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView parent, View v, int position,
				long id) {
			if (v != null) {
				AlarmDialogDataItem.ViewItem item = (AlarmDialogDataItem.ViewItem) v;
				if (item != null) {
					clearAllSelection();
					item.getDataItem().setSelected(true);
					btnAlarmReminderClear.requestFocus();
				}
			}
			updateBottomButtonsState();
		}
	};

	// android.widget.AdapterView.OnItemSelectedListener
	@SuppressWarnings("all")
	private AdapterView.OnItemSelectedListener mListViewOnItemSelected = new AdapterView.OnItemSelectedListener() {
		public void onItemSelected(AdapterView parent, View v, int position,
				long id) {
			updateBottomButtonsState();
		}

		public void onNothingSelected(AdapterView arg0) {
			updateBottomButtonsState();
		}
	};

	public void clearAllSelection() {
		for (int i = 0; i < vecAlarmItems.size(); i++) {
			AlarmDialogDataItem item = vecAlarmItems.get(i);
			if (item != null)
				item.setSelected(false);
		}
	}

	public int getAlarmItemSelectedCount() {
		int iCount = 0;
		for (int i = 0; i < vecAlarmItems.size(); i++) {
			AlarmDialogDataItem item = vecAlarmItems.get(i);
			if ((item != null) && (item.isSelected()))
				iCount++;
		}
		return iCount;
	}

	public AlarmDialogDataItem getSelectedItem() {
		for (int i = 0; i < vecAlarmItems.size(); i++) {
			AlarmDialogDataItem item = vecAlarmItems.get(i);
			if ((item != null) && (item.isSelected()))
				return item;
		}
		return null;
	}

	public void updateBottomButtonsState() {
		int iSelectedCount = getAlarmItemSelectedCount();
		boolean bEnabled = (iSelectedCount == 1);
		btnAlarmReminderClear.setEnabled(bEnabled);
		btnAlarmReminderSnooze.setEnabled(bEnabled);
		btnAlarmReminderOpen.setEnabled(bEnabled);
	}

	public void removeAlarmItem(AlarmDialogDataItem item) {
		vecAlarmItems.remove(item);
		setListWithDataAdapter();
		selectFirstListItem();
		updateHeaderInfo();
		updateBottomButtonsState();
	}

	public AlarmDialogDataItem getSelectedAlarmItemToProcess() {
		AlarmDialogDataItem item = getSelectedItem();
		if (item != null) {
			return item;
		}
		return null;
	}

	public boolean noAlarmItems() {
		return (vecAlarmItems.size() == 0);
	}

	private View.OnClickListener mBtnOnClick_CLEAR = new View.OnClickListener() {
		public void onClick(View arg0) {
			AlarmDialogDataItem item = getSelectedAlarmItemToProcess();
			if (item != null) {
				alarmsManager.putAlarmToProcess(item,
						AlarmsManager.iAction_CLEAR);
				removeAlarmItem(item);
			}
			closeDialogIfEmpty();
		}
	};

	private View.OnClickListener mBtnOnClick_SNOOZE = new View.OnClickListener() {
		public void onClick(View arg0) {
			AlarmDialogDataItem item = getSelectedAlarmItemToProcess();
			if (item != null) {
				alarmsManager.putAlarmToProcess(item,
						AlarmsManager.iAction_SNOOZE);
				removeAlarmItem(item);
			}
			closeDialogIfEmpty();
		}
	};

	private void closeDialogIfEmpty() {
		if (noAlarmItems()) {
			// post delay task
			handlerCloseDialog.removeCallbacks(handlerUpdateDialogTask);
			handlerCloseDialog.postDelayed(handlerUpdateDialogTask, 1000);
		}
	}

	private Runnable handlerUpdateDialogTask = new Runnable() {
		public void run() {
			AlarmDialog.this.finish();
		}
	};

	public void openDataForEdit(AlarmDialogDataItem item) {
		bundleOtherDataStartup.clear();
		if (item.getOrderFilter() == AlarmDataViewItem.iOrderAppts)
			openActivity(0,
					"android.intent.action.TaskManager.ACTION_MODE_EDIT_APPOINTMENT",
					item.getID());
		if (item.getOrderFilter() == AlarmDataViewItem.iOrderTasks)
			openActivity(0,
					"android.intent.action.TaskManager.ACTION_MODE_EDIT_TASK", item
							.getID());
	}

	private View.OnClickListener mBtnOnClick_OPEN = new View.OnClickListener() {
		public void onClick(View arg0) {
			AlarmDialogDataItem item = getSelectedAlarmItemToProcess();
			if (item != null) {
				openDataForEdit(item);
				AlarmDialog.this.finish();
			}
		}
	};

	public boolean updateReminderService(String sKey) {
		Bundle args = new Bundle();

		args.putBoolean(sKey, true);
		// put additional prefs
		args.putBoolean("b24HourMode", prefs.b24HourMode);
		args.putInt("iFirstDayOfWeek", prefs.iFirstDayOfWeek);

		// update service
		Intent intent = new Intent(this, AlarmService.class);
		intent.putExtras(args);
		ComponentName cpn = startService(intent);
		return (cpn != null);
	}

	@Override
	protected void restoreStateFromFreeze() {

	}

}
